<!-- SUCCESSS :D -->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script type="text/javascript" src="js/jquery.js"></script>
        <script src="js/cordova.js"></script>
        <script src="js/cordova_plugins.js"></script>

        <title>Hello World</title>
    </head>
 
    <body>
        <div class="app">
            <h1>PhoneGap</h1>
            <div id="deviceready" class="blink">
 
            </div>
        </div>
        <div id="xyz" style="font-size:40px">
            
        </div>
    </body>
    <script type="text/javascript" src="phonegap.js"></script>
    <script type="text/javascript">
        document.addEventListener('deviceready', onDeviceReady, false);
        var _url;
        
        function downloadFile() {
            // alert('downloadFile');
            window.requestFileSystem(
                LocalFileSystem.PERSISTENT,
                0,
                onRequestFileSystemSuccess,
                fail
            );
        }
        
        function onRequestFileSystemSuccess(fileSystem) {
            // alert('onRequestFileSystemSuccess');
            fileSystem.root.getFile(
                'dummy.html',
                {create: true, exclusive: false},
                onGetFileSuccess,
                fail
            );
        }
        
        function onGetFileSuccess(fileEntry) {
            alert('onGetFileSuccess');
            var path = fileEntry.toURL().replace('dummy.html', '');
            var fileTransfer = new FileTransfer();
            fileEntry.remove();
            
            fileTransfer.download(
                'http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf',
                // 'http://phanhuutho.com/thesis/epub/index/export-epub/id/'+ book_id,
                path + 'abc.pdf',
                function(file) {
                    alert('download complete: ' + file.toURI());
                    showLink(file.toURI());
                },
                function(error) {
                    alert('download error source ' + error.source);
                    alert('download error target ' + error.target);
                    alert('upload error code: ' + error.code);
                }
            );
        }
        
        function showLink(url) {
            alert("itms-bookss://" +url);
            _url = url;
            $("#xyz").append('<a href="' + url +'">file:///var</a><br/>');
            $("#xyz").append('<a href="itms-bookss://' + url.substr(5) +'">///var</a><br/>');
            $("#xyz").append('<a href="itms-bookss://' + url.substr(7) +'">/var</a><br/>');
            $("#xyz").append('<a href="itms-bookss://' + url.substr(8) +'">var</a><br/>');
            LaunchFile(url);
        }
        
        function fail(evt) {
            alert(evt.target.error.code);
        }
        
        /* When this function is called, PhoneGap has been initialized and is ready to roll */
        function onDeviceReady() {
            alert('device ready!');
            downloadFile();
        }
    </script>
</html>